import mitmproxy
from mitmproxy import ctx, http
import json

# ReWrite

num = 0


def response(flow: mitmproxy.http.HTTPFlow):
    """
    修改response中的股票涨跌数据测试颜色
    """
    global num
    color_test = {
        0: "-0.01",
        1: "0.00",
        2: "0.01"
    }

    if "v5/stock/batch/quote.json" in flow.request.pretty_url and "x=" in flow.request.pretty_url:
        datas = json.loads(flow.response.content)
        datas["data"]["items"][0]["quote"]["name"] = "Mitm测试"
        datas["data"]["items"][0]["quote"]["percent"] = color_test[num % 3]
        flow.response.text = json.dumps(datas)
        num += 1
    else:
        ctx.log(f"url is : {flow.request.url}")
